﻿using StackOverflow.DalFactory;
using StackOverflow.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace StackOverflow.BLL
{
    public abstract class BaseServices<T> where T : class, new()
    {
        public IDbSession CurrentDbSession
        {
            get => DbSessionFactory.CreateDbSession();
        }


        public IBaseDal<T> CurrentDal { get; set; }

        /// <summary>
        /// 虚方法
        /// </summary>
        /// Author:bishisan
        /// Date:2017/8/31
        public abstract void SetCurrentDal();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// Author:bishisan
        /// Date:2017/8/31
        public BaseServices() {
            SetCurrentDal();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="whereLambda">The where lambda.</param>
        /// <returns></returns>
        /// Author:bishisan
        /// Date:2017/8/31
        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.LoadEntities(whereLambda);
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <typeparam name="s"></typeparam>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="totalCount">The total count.</param>
        /// <param name="whereLambda">The where lambda.</param>
        /// <param name="orderByLambda">The order by lambda.</param>
        /// <param name="isAsc">if set to <c>true</c> [is asc].</param>
        /// <returns></returns>
        /// Author:bishisan
        /// Date:2017/8/31
        public IQueryable<T> LoadPageEntities<s>(int pageSize, int pageIndex, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderByLambda, bool isAsc)
        {
            return CurrentDal.LoadPageEntities<s>(pageSize, pageIndex, out totalCount, whereLambda, orderByLambda, isAsc);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        /// Author:bishisan
        /// Date:2017/8/31
        public bool DeleteEntity(T entity)
        {
            CurrentDal.DeleteEntity(entity);
            return CurrentDbSession.SaveChanges();
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        /// Author:bishisan
        /// Date:2017/8/31
        public bool EditEntity(T entity)
        {
            CurrentDal.EditEntity(entity);
            return CurrentDbSession.SaveChanges();
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        /// Author:bishisan
        /// Date:2017/8/31
        public T AddEntity(T entity)
        {
            CurrentDal.AddEntity(entity);
            CurrentDbSession.SaveChanges();
            return entity;
        }

    }
}
